﻿using System;
using System.Web.UI.WebControls;
using Alserve.Ffl;
using Alserve.Ffl.Data;

public partial class ffl_admin_UserAdmin : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!Security.IsAdmin(Page))
        {
            Response.Redirect("~//ffl//anonymous//default.aspx");
        }

        if (!IsPostBack)
        {
            Alserve.Ffl.Data.User thisUser = new Alserve.Ffl.Data.User();
            thisUser.Query.AddOrderBy(Alserve.Ffl.Data.User.ColumnNames.Surname, MyGeneration.dOOdads.WhereParameter.Dir.ASC);

            if (thisUser.Query.Load())
            {
                UserRepeater.DataSource = thisUser.DefaultView;
                UserRepeater.DataBind();
            }
        }
    }

    protected void UserRepeater_ItemCommand(object source, RepeaterCommandEventArgs e)
    {
        Alserve.Ffl.Data.User thisUser = new Alserve.Ffl.Data.User();
        thisUser.Where.Id.Value = Convert.ToInt32(((HiddenField)e.Item.FindControl("UserIdHidden")).Value);

        if (thisUser.Query.Load())
        {
            thisUser.Email = ((TextBox)e.Item.FindControl("EmailTextBox")).Text;
            thisUser.Forename = ((TextBox)e.Item.FindControl("ForenameTextBox")).Text;
            thisUser.Surname = ((TextBox)e.Item.FindControl("SurnameTextBox")).Text;
            thisUser.TeamName = ((TextBox)e.Item.FindControl("TeamNameTextBox")).Text;
            thisUser.Password = ((TextBox)e.Item.FindControl("PasswordTextBox")).Text;
            thisUser.Paid = ((CheckBox)e.Item.FindControl("PaidCheckBox")).Checked;
            thisUser.Save();
        }
    }

    protected Int32 GetUserPlayerCount(Int32 userId)
    {
        Int32 returnValue = 0;

        _UserPlayerSquad thisSquad = new _UserPlayerSquad();
        thisSquad.Where.UserId.Value = userId;

        if (thisSquad.Query.Load())
            returnValue = thisSquad.RowCount;

        return returnValue;
    }

    protected Boolean GetUserPlayerDuplicates(Int32 userId)
    {
        Boolean returnValue = false;
        Int32 lastClubId = 0;

        _UserPlayerSquad thisSquad = new _UserPlayerSquad();
        thisSquad.Where.UserId.Value = userId;
        thisSquad.Query.AddOrderBy(_UserPlayerSquad.ColumnNames.ClubId, MyGeneration.dOOdads.WhereParameter.Dir.ASC);

        if (thisSquad.Query.Load())
        {
            while (!thisSquad.EOF)
            {
                if (lastClubId == thisSquad.ClubId)
                {
                    returnValue = true;
                    break;
                }
                lastClubId = thisSquad.ClubId;
                thisSquad.MoveNext();
            }
        }

        return returnValue;
    }
}
